package graphics;

import graphics.gui.MainWindow;
import graphics.rendering.GraphicsFactory;
import graphics.rendering.GraphicsPipeline;
import graphics.rendering.shaders.ShaderManager;
import core.Lunecy;
import util.common.AbstractManager;

/**
 * The graphics manager provides an abstraction on the low-level
 * OpenGL and windowing system. 
 * @author Joel Rausch
 */
public class GraphicsManager extends AbstractManager {
	
	/**
	 * Graphics pipeline which renders into window.
	 */
	private GraphicsPipeline graphics;
	
	/**
	 * Display window.
	 */
	private MainWindow window;

	/** Width of screen. */
	private int width;
	
	/** Height of screen. */
	private int height;

	@Override
	public void startup() throws Exception {
		width = parent.config.getIntProperty("width", 1024);
		height = parent.config.getIntProperty("height", 768);
		
		window = new MainWindow(width, height, false);
		window.setup();
		
		graphics = GraphicsFactory.create(this);
		graphics.setup();
	}

	@Override
	public void shutdown() {
		graphics.shutdown();
		window.shutdown();
	}
	
	/**
	 * Renders a single frame.
	 */
	public void renderFrame() {
		// Obtain and render geometry
		parent.scene.addGeometry(graphics);
		graphics.processPrimitives();
		graphics.flushPipe();
		
		// Window updates
		parent.exit = window.exitRequested();
		window.update();
		window.synch(60);
	}
	
	public int getHeight() {
		return height;
	}
	
	public int getWidth() {
		return width;
	}
}
